#include "manage_face.h"
#include "ui_manage_face.h"

manage_face::manage_face(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::manage_face)
{
    ui->setupUi(this);

    setWindowIcon(QIcon(":/img/icon.jpg"));

    setWindowTitle("天天考试系统");

    ui->stackedWidget->setCurrentWidget(ui->infoPage);

    m_dir = QCoreApplication::applicationDirPath();
}

/* 管理员界面关闭事件 */
void manage_face::closeEvent(QCloseEvent *event)
{
    emit close();
}

void manage_face::on_infoBtn_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->infoPage);
}

void manage_face::on_enterBtn_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->enterPage);
}

void manage_face::on_removeBtn_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->removePage);
}

/* 点击录入按钮 */
void manage_face::on_enter_Btn_clicked()
{
    handleFileInfo();

    if (ui->le_exam->text() == "" && ui->le_textname->text() == "")
    {
        QMessageBox::warning(this, "录入失败", "请填写完整信息");
        return;
    }

    if (fileName.isEmpty() == true)
    {
        QMessageBox::warning(this, "录入失败", "试卷文件不存在");
        return;
    }

    QFile file(m_dir + "/Info.txt");
    if (file.exists() == false)
    {
        qDebug() << "创建试卷信息文件" << endl;
    }

    file.open(QIODevice::ReadOnly);

    QStringList lines;  //字符串链表

    while (file.atEnd() == false) //文件没结束
    {
        QString line = file.readLine(); // 读取每一行字符串
        lines.append(line);    //读取每一行字符串到链表中
    }
    file.close();

    QString newline = ui->le_exam->text() + "+" + ui->le_textname->text() + "+" + fileName + "\n";
    lines.append(newline);

    file.open(QIODevice::WriteOnly);

    QTextStream out(&file);
    // 设置编码为 UTF-8
    out.setCodec("UTF-8");
    for (int idx = 0; idx < lines.size(); idx++)
    {
        out << lines[idx];
    }

    file.close();

    QMessageBox::warning(this, "录入成功", "试卷已录入");
    ui->le_exam->clear();
    ui->le_textname->clear();
    ui->le_path->clear();
}

/* 点击更多按钮 */
void manage_face::on_moreBtn_clicked()
{
    fileName = QFileDialog::getOpenFileName(this, tr("Open File"), m_dir, tr("Excel Files (*.xls *.xlsx)"));
    qDebug() << fileName << endl;
    ui->le_path->setText(fileName);
}

/* 点击删除按钮 */
void manage_face::on_remove_Btn_clicked()
{
    if (ui->le_re_exam->text() == "" && ui->le_re_textname->text() == "")
    {
        QMessageBox::warning(this, "删除失败", "请填写完整信息");
        return;
    }

    QFile file(m_dir + "/Info.txt");
    if (file.exists() == false)
    {
        qDebug() << "创建试卷信息文件" << endl;
    }

    file.open(QIODevice::ReadOnly);

    QStringList lines;  //字符串链表

    while (file.atEnd() == false) //文件没结束
    {
        QString line = file.readLine(); // 读取每一行字符串
        lines.append(line);    //读取每一行字符串到链表中
    }
    file.close();

    // 查找并删除目标行
    bool found = false;
    for (int idx = 0; idx < lines.size(); ++idx)
    {
        QString data = lines[idx];
        if (lines[idx].split("+")[0] == ui->le_re_exam->text() && lines[idx].split("+")[1] == ui->le_re_textname->text())
        {
                lines.removeAt(idx);    // 删除该行
                found = true;
                break; // 只需要删除第一个匹配项
        }
    }

    if (!found) //没找到
    {
        QMessageBox::warning(this, "删除失败", "该试卷不存在");
        return;
    }

    file.open(QIODevice::WriteOnly);

    QTextStream out(&file);
    // 设置编码为 UTF-8
    out.setCodec("UTF-8");
    for (int idx = 0; idx < lines.size(); idx++)
    {
        out << lines[idx];
    }

    file.close();

    QMessageBox::warning(this, "删除成功", "试卷已删除");
    ui->le_re_exam->clear();
    ui->le_re_textname->clear();
}

/* 处理文件信息 */
void manage_face::handleFileInfo()
{
    for (int idx = fileName.size(); idx > 0; idx--)
    {
        if (fileName[idx] == "/")
        {
            fileName.insert(idx + 1, '/');
        }
    }
    qDebug() << fileName << endl;
}

manage_face::~manage_face()
{
    delete ui;
}
